package com.glory.hikvisionmobile.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.ToArrayListAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.TrackIn.ToArrayBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * FileName: Lot_ToArrayActivity
 * Author: XJH
 * Date: 2020/5/9 11:50
 * Description: 分片作业
 * History:
 */
public class Lot_ToArrayActivity extends BaseActivity {
    @BindView(R.id.etLot)
    EditText etLot;
    @BindView(R.id.tvStats)
    TextView tvStats;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_check)
    TextView list_check;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.list_tv6)
    TextView listTv6;
    @BindView(R.id.list_titleLayout)
    LinearLayout listTitleLayout;
    @BindView(R.id.rvToArrayList)
    RecyclerView rvToArrayList;
    @BindView(R.id.bottom_tv1)
    TextView bottomTv1;
    @BindView(R.id.bottom_tv2)
    TextView bottomTv2;
    @BindView(R.id.bottom_tv3)
    TextView bottomTv3;
    @BindView(R.id.bottom_tv4)
    TextView bottomTv4;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvNum)
    TextView tvNum;
    @BindView(R.id.tvPart)
    TextView tvPart;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvPartDesc)
    TextView tvPartDesc;

    private ToArrayListAdapter toArrayListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_toarray;
    }

    @Override
    protected void initView() {
        initListTitle();
        initEdit(etLot, s -> initData(s,"query"), false);

        String lotID = getIntent().getExtras().getString("lotID");
        if (!FreeApi_StringUtils.isEmpty(lotID)){
            etLot.setText(lotID);
            initData(lotID,"query");
        }
        rvToArrayList.setLayoutManager(new LinearLayoutManager(Lot_ToArrayActivity.this));
        rvToArrayList.setItemAnimator(new DefaultItemAnimator());
        rvToArrayList.addItemDecoration(new DividerItemDecoration(Lot_ToArrayActivity.this, 1));
        toArrayListAdapter = new ToArrayListAdapter(null);
        toArrayListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        toArrayListAdapter.isFirstOnly(true);
        rvToArrayList.setAdapter(toArrayListAdapter);
    }

    private void initData(String lotID,String actionType) {
        JsonObject map = new JsonObject();
        map.addProperty("actionType", actionType);
        map.addProperty("lotId", lotID);
        NetUtils.requestDara("HIK.PdaToArray", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<ToArrayBean>>
                (new TypeToken<BaseResponseBean<ToArrayBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<ToArrayBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    ToArrayBean toArrayBean = response.body().getBody();
                    setData(toArrayBean);
                }



            }

            @Override
            public void onError(Response<BaseResponseBean<ToArrayBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaToArray");


            }
        });
    }

    private void setData(ToArrayBean toArrayBean) {
        toArrayListAdapter.setButton(bottomTv1, bottomTv2, bottomTv3, toArrayBean.getHoldState().equals("On"));
        tvStats.setText(toArrayBean.getState());
        tvNum.setText(toArrayBean.getMainQty());
        tvPart.setText(toArrayBean.getPartName());
        tvStep.setText(toArrayBean.getCurrentStepDesc());
        tvPartDesc.setText(toArrayBean.getPartDesc());
        //设置数据
        List<ToArrayBean.ToArrayList> xPdaLotToArray = toArrayBean.getxPdaLotToArrays();
        if (xPdaLotToArray.size() <= 0) {
            showToast("暂无数据", false);
        }
        toArrayListAdapter.setNewData(xPdaLotToArray);
        toArrayListAdapter.clear();
        setBottomEnable();
    }

    private void initListTitle() {
        listTv1.setText("序号");
        listTv2.setText("片号");
        listTv3.setText("菜单");
        listTv4.setText("处理组");
        listTv5.setText("状态");
        listTv4.setVisibility(View.VISIBLE);
        listTv3.setVisibility(View.VISIBLE);
        listTv2.setVisibility(View.VISIBLE);
        listTv1.setVisibility(View.VISIBLE);
        listTv5.setVisibility(View.VISIBLE);
        list_check.setVisibility(View.VISIBLE);

        bottomTv1.setText("进站");
        bottomTv2.setText("出站");
        bottomTv3.setText("退站");
        bottomTv4.setVisibility(View.GONE);
        setBottomEnable();
    }

    private void setBottomEnable() {
        bottomTv1.setEnabled(false);
        bottomTv2.setEnabled(false);
        bottomTv3.setEnabled(false);
        bottomTv1.setBackgroundColor(getResources().getColor(R.color.free_ui_blue_sky_text_gray));
        bottomTv2.setBackgroundColor(getResources().getColor(R.color.free_ui_blue_sky_text_gray));
        bottomTv3.setBackgroundColor(getResources().getColor(R.color.free_ui_blue_sky_text_gray));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("分片作业");
        return new FreeUI_AddViewEntity("header", llMain);
    }


    @OnClick({R.id.bottom_tv1, R.id.bottom_tv2, R.id.bottom_tv3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bottom_tv1:
                submit("trackIn");
                break;
            case R.id.bottom_tv2:
                submit("trackOut");
                break;
            case R.id.bottom_tv3:
                submit("abort");
                break;
        }
    }

    private void submit(String actionType) {
        DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, new DialogUtils.signCallback() {
            @Override
            public void onSignSuccess() {
                JsonObject map = new JsonObject();
                JsonObject json = new JsonObject();
                json.add("XPdaLotToArray", new JsonParser().parse(new Gson().toJson(toArrayListAdapter.getSelectorData())).getAsJsonArray());
                map.addProperty("lotId", etLot.getText().toString().trim());
                map.addProperty("actionType", actionType);
                map.add("xPdaLotToArrays", json);

                NetUtils.requestDara("HIK.PdaToArray", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<JsonObject>>(
                        new TypeToken<BaseResponseBean<JsonObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JsonObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                            switch (actionType) {
                                case "trackIn":
                                    showConfirmDialog("进站成功");
                                    break;
                                case "trackOut":
                                    showConfirmDialog("出站成功");
                                    break;
                                case "abort":
                                    showConfirmDialog("退站成功");
                                    break;
                            }
                        }

                        initData(etLot.getText().toString(),"query");
                        toArrayListAdapter.clear();

                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JsonObject>> response) {
                        super.onError(response);
                        loadError(response.getException(), "HIK.PdaToArray");

                    }
                });

            }
        }, getSupportFragmentManager());


    }

}
